package com.wcfsy.wechat.wx.util.excel;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;


public class ExcelView extends AbstractExcelView {

    private List<?> data;

    private Map<String, String> headerMap;

    private String fileName;

    public ExcelView(List<?> data, Map<String, String> headerMap, String fileName) {
        super();
        this.data = data;
        this.headerMap = headerMap;
        this.fileName = fileName;
    }

    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {

        ExcelUtil excelUtil = new ExcelUtil(workbook);
        excelUtil.outputWorkBook(data, headerMap);
        String newFileName = encodeFilename(fileName, request);

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + newFileName + ".xls");

    }

    /**
     * 处理中文文件名
     * 
     * @param filename
     * @param request
     * @return
     */
    public String encodeFilename(String filename, HttpServletRequest request) {
        /*
         * 获取客户端浏览器和操作系统信息
         * 在IE浏览器中得到的是：User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
         * 在Firefox中得到的是：User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717
         * Chrome浏览器 :User-Agent=Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95
         * Safari/537.36
         */
        String agent = request.getHeader("USER-AGENT");
        String newFileName = "";
        try {
            if (agent.indexOf("MSIE") != -1) {// IE
                newFileName = URLEncoder.encode(filename, "UTF-8");
                newFileName = StringUtils.replace(newFileName, "+", "%20");
                if (newFileName.length() > 150) {
                    newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
                    newFileName = StringUtils.replace(newFileName, " ", "%20");
                }
            } else if (agent.indexOf("Chrome") != -1) {// Chrome
                newFileName = URLEncoder.encode(filename, "UTF-8");
            } else {// FireFox
                newFileName = MimeUtility.encodeText(filename, "UTF-8", "B");
            }
        } catch (Exception ex) {
            newFileName = filename;
        }
        return newFileName;
    }

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }

    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

}
